package com.lyf.boot.core.model;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lyf.boot.constant.Constant;
import lombok.Data;

import java.io.Serial;
import java.io.Serializable;

/**
 * @author Luyufan
 * @date 2023/7/3 14:41
 */
@Data
public class BasePageDto implements Serializable {

    @Serial
    private static final long serialVersionUID = -1328914770289034924L;
    /**
     * 页数
     */
    private Integer pageNum;

    /**
     * 每页条数
     */
    private Integer pageSize;

    /**
     * 排序字段
     */
    private String field;

    /**
     * 排序方式
     */
    private String order;

    public <T> Page<T> of(){
        Page<T> page = new Page<>(pageNum, pageSize);
        if(StrUtil.isNotBlank(field) && StrUtil.isNotBlank(order)){
            OrderItem orderItem = new OrderItem();
            orderItem.setColumn(StrUtil.toUnderlineCase(field));
            orderItem.setAsc(StrUtil.equals(order, Constant.PAGE_ORDER_ASC));
            page.addOrder(orderItem);
        }
        return page;
    }

    public <T> Page<T> of(String field, boolean asc){
        this.field = field;
        this.order = asc ? Constant.PAGE_ORDER_ASC : Constant.PAGE_ORDER_DESC;
        return this.of();
    }
}
